package com.ebao.listener;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.ebao.constants.WebConstants;
import com.ebao.po.doctor.TDoctor;
import com.ebao.po.user.TUser;

/**
 * @author snow
 * @Date 2012-9-4
 */
public class OnlineListener implements HttpSessionListener, ServletContextListener {

	private ServletContext servletContext;
	
	// Map<sessionId,email>
	private Map<String,String> userMap = new HashMap<String,String>();
	private Map<String,String> doctorMap = new HashMap<String,String>();
	
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("前台   OnlineListener --> 监听注销");
		userMap = null;
		doctorMap = null;
		
		
	}

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("前台   OnlineListener --> 监听初始化启动");
		servletContext = sce.getServletContext();
		
		
	}

	@Override
	public void sessionCreated(HttpSessionEvent event) {
		System.out.println("前台   OnlineListener --> 有用户登录了");
//		TUser user = (TUser) event.getSession().getAttribute(WebConstants.LOGIN_USER);
//		if(user != null){
//			userMap.put(event.getSession().getId(), user.getEmail());
//			System.out.println("user登录:" + user.getEmail());
//		}
//		
//		TDoctor doctor = (TDoctor) event.getSession().getAttribute(WebConstants.LOGIN_DOCTOR);
//		if(doctor != null){
//			doctorMap.put(event.getSession().getId(), doctor.getEmail());
//			System.out.println("doctor登录:" + doctor.getEmail());
//		}
		
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent event) {
		System.out.println("前台   OnlineListener --> 有用户退出了");
//		TUser user = (TUser) event.getSession().getAttribute(WebConstants.LOGIN_USER);
//		if(user != null){
//			userMap.remove(event.getSession().getId());
//			System.out.println("user退出:" + user.getEmail());
//		}
//		
//		TDoctor doctor = (TDoctor) event.getSession().getAttribute(WebConstants.LOGIN_DOCTOR);
//		if(doctor != null){
//			doctorMap.remove(event.getSession().getId());
//			System.out.println("doctor退出:" + doctor.getEmail());
//		}
		
	}

}
